package com.dzx.awt;

import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * @author dzx
 * @ClassName:
 * @Description:
 * @date 2019年07月22日 09:50:33
 */
public class MenuDemo {
    private Frame f;
    private MenuBar mb;//创建菜单顶级容器
    private Menu file;//二级菜单
    private Menu edit;
    private MenuItem newItem;//最底级菜单
    private MenuItem saveItem;
    private MenuItem exitItem;
    private MenuItem copyItem;
    private MenuItem pasteItem;


    public MenuDemo() {
        init();
    }

    public void init() {
        mb = new MenuBar();
        f = new Frame("Test");
        f.setBounds(300,
                400, 500, 600);
        file = new Menu("文件");
        edit = new Menu("编辑");
        newItem = new MenuItem("新建", new MenuShortcut(KeyEvent.VK_N));
        saveItem = new MenuItem("保存", new MenuShortcut(KeyEvent.VK_S));//添加快捷键
        exitItem = new MenuItem("退出", new MenuShortcut(KeyEvent.VK_X));
        copyItem = new MenuItem("复制", new MenuShortcut(KeyEvent.VK_C));
        pasteItem = new MenuItem("粘贴", new MenuShortcut(KeyEvent.VK_V));
        file.add(newItem);
        file.add(saveItem);
        file.add(exitItem);

        edit.add(copyItem);
        edit.add(pasteItem);

        mb.add(file);
        mb.add(edit);

        f.setMenuBar(mb);
        f.setVisible(true);
    }

    public static void main(String[] args){
        new MenuDemo();
    }

}
